package nxu.it.controller;
import nxu.it.model.Todo;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Param;
import org.noear.solon.boot.web.MimeType;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.core.handle.ModelAndView;
import java.util.*;
import static io.undertow.util.StatusCodes.*;

@Controller
public class TodoController {
    List<Todo> todoList =new ArrayList<>();
    {
        todoList.add(new Todo("吃饭",true));
        todoList.add(new Todo("睡觉",true));
        todoList.add(new Todo("做梦",true));
    }
    @Mapping(value = "/todo/list",method= MethodType.GET,produces = MimeType.TEXT_HTML_VALUE)
    public ModelAndView list(){
        ModelAndView mv=new ModelAndView("todo/list.ftl");
        mv.put("todoList",todoList);
        return mv;
    }
    @Mapping(value = "/todo/save",method= MethodType.POST,produces = MimeType.TEXT_HTML_VALUE)
    public void save(Context ctx, String content){
        if(content==null||content.trim().isEmpty()){
            ctx.redirect("/todo/list", SEE_OTHER);
            return;
        }
        Todo todo=new Todo(content);
        todoList.add(todo);
        ctx.redirect("/todo/list", SEE_OTHER);
    }
    @Mapping(value = "/todo/check",method= MethodType.GET,produces = MimeType.TEXT_HTML_VALUE)
    public void check(Context ctx,@Param int id){
        boolean exist=false;
        for(int i=0;i<todoList.size();i++){
            if(todoList.get(i).getId().intValue()==id){
                todoList.get(i).setComplete(!todoList.get(i).getComplete());
                exist=true;
                break;
            }
        }
        ctx.redirect("/todo/list", SEE_OTHER);
    }
    @Mapping(value = "/todo/edit",method= MethodType.GET,produces = MimeType.TEXT_HTML_VALUE)
    public ModelAndView edit(Context ctx, @Param int id){
        Optional<Todo> todoOptional=todoList.stream().filter(todo -> todo.getId().intValue()==id).findAny();
        if(todoOptional.isEmpty()){ ctx.status(NOT_FOUND);
            return new ModelAndView("not-found.ftl",Map.of());
        }
        ModelAndView mv=new ModelAndView("todo/edit.ftl", Map.of("todo",todoOptional.get()));
        return mv;
    }
    @Mapping(value = "/todo/update",method= MethodType.POST,produces = MimeType.TEXT_HTML_VALUE)
    public void update(Context ctx,@Param Todo todo){
        todoList.stream().filter(todo1 -> todo1.getId().intValue()==todo.getId().intValue()).findAny().ifPresent
                (todo1 ->{todo1.setContent(todo.getContent());
                    todo1.setComplete(todo.getComplete());
                });

        ctx.redirect("/todo/list", SEE_OTHER);
    }
    @Mapping(value = "/todo/delete",method= MethodType.GET,produces = MimeType.TEXT_HTML_VALUE)
    public void delete(Context ctx,@Param int id){
        Iterator<Todo> iter=todoList.iterator();
        while(iter.hasNext()){
            if(iter.next().getId().intValue()==id){
                iter.remove();
                break;
            }
        }
        ctx.redirect("/todo/list", SEE_OTHER);
    }
}